home *** CD-ROM | disk | FTP | other *** search
/ PCMania 75 / PCMania CD75_1.iso / pcmania / metafo75 / EXAMPLES / SIMPLE.C < prev    next >
C/C++ Source or Header  |  1993-01-06  |  2KB  |  85 lines

  1. /*     SIMPLE.C - a simple demonstration PCRobots Robot in Turbo C    */
  2. /*        Written by P.D.Smith - July 1992            */
  3.  
  4. #include <stdlib.h>
  5. #include <time.h>
  6.  
  7. /* Include the PCROBOTS header file for the special function definitions */
  8. #include "pcrobots.h"
  9.  
  10. /*
  11.  * Define possible angles of motion
  12.  * x,y are x,y positions on the local map
  13.  * angle is the angle of motion to specify
  14.  */
  15. struct     {
  16.      int    x,
  17.         y,
  18.         angle;
  19.     }angle[8]={{5,4,0},{5,5,45},{4,5,90},{3,5,135},{3,4,180},
  20.            {3,3,225},{4,3,270},{5,3,315}};
  21.  
  22. void    main(void)
  23. {
  24.  char    local_map[9][9];     /* Buffer to hold the local map */
  25.  int    x_pos,            /* Current X and Y coordinates of robot */
  26.     y_pos;
  27.  int    go_dir;            /* current direction (index to angle[]) */
  28.  int    scan_angle=0;        /* Current scanning angle */
  29.  int    range;            /* range returned from the scan routine */
  30.  int    try;
  31.  
  32. /* Configure robot - all normal settings in this case
  33.  * This *MUST* be the first special function called by the robot   */
  34.  configure(2,2,2,2,2,0);
  35.  
  36.  randomize();
  37.  
  38.  go_dir=random(8);        /* Select initial direction */
  39.  
  40. /* Note - this is an *INFINITE* loop - the PCROBOTS executive program
  41.    kills the robot off when necessary*/
  42.  while(1)
  43.  {
  44. /* Get the current position */
  45.   getxy(&x_pos,&y_pos);
  46.  
  47. /* Get a map of the local area */
  48.   get_local_map((char *)local_map);
  49.  
  50. /* Quick and easy way of dealing with the robot being near an outside wall */
  51.   if (x_pos<20)
  52.    go_dir=(random(3)-1)%8;
  53.   if (x_pos>=980)
  54.    go_dir=random(3)+3;
  55.   if (y_pos<20)
  56.    go_dir=random(3)+1;
  57.   if (y_pos>=980)
  58.    go_dir=random(3)+5;
  59.  
  60.   try=0;
  61. /* Look for a square which hasn't got a wall in it (ignore traps in this
  62.  * simple program) */
  63.   while (local_map[angle[go_dir].y][angle[go_dir].x] == ARENA_WALL)
  64.   {
  65.    go_dir=random(8);
  66.  
  67. /* Failsafe check to avoid the program getting trapped here */
  68.    if (try++ > 10)
  69.     break;
  70.   }
  71.  
  72. /* Move in the new direction */
  73.   movement(50,angle[go_dir].angle);
  74.  
  75. /* Check if target found, shoot at it if so, otherwise go to next angle */
  76.   if (scan(scan_angle,5,&range)>=0)
  77.    shoot(scan_angle,range);
  78.   else
  79.    scan_angle=(scan_angle+5)%360;
  80.  
  81.  }
  82. }
  83.  
  84.  
  85.